package com.yidu.view;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yidu.bean.TbManager;
import com.yidu.bean.TbManagerRole;
import com.yidu.bean.TbRole;
import com.yidu.bean.TbRoleModule;
import com.yidu.bean.TbShop;
import com.yidu.commom.Tool;
import com.yidu.message.Message;
import com.yidu.message.Pages;
import com.yidu.service.TbManagerRoleService;
import com.yidu.service.TbRoleModuleService;
import com.yidu.service.TbRoleService;

/**
 * 类的描述--
 * @author 楠竹
 * @date 2018年7月24日
 * @version 1.0
 */
@Controller
@RequestMapping("/role")
public class TbRoleAction {

	Logger logger = Logger.getLogger(TbRoleAction.class);
	//消息信息类
	Message msg = new Message();

	//json	
	ObjectMapper mapper = new ObjectMapper();

	@Resource
	private TbRoleService roleService;

	@Resource
	private TbManagerRoleService managerRoleService;

	@Resource
	private TbRoleModuleService roleModuleService;


	@ResponseBody
	@RequestMapping("/findAll")
	private Object findAll(HttpSession session){

		logger.info("进入查询所有角色...");
		List<TbRole> list = roleService.findAll();

		List<Object> lists = new ArrayList<>();
		
		TbManager man = (TbManager) session.getAttribute("showManager");
		
		lists.add(man);
		lists.add(list);
		
		return lists;

	}

	@ResponseBody
	@RequestMapping("/findManagerRole")
	private Object findManagerRole(String manId) throws JsonProcessingException{ 
		logger.info("进入查询所有角色...");
		logger.info(manId);
		List<TbManagerRole> list = managerRoleService.findRole(manId);

		logger.info(mapper.writeValueAsString(list));

		return list; 
	}


	/**
	 * 根据ID查询对象类型
	 * 
	 * @param id
	 * @param response
	 * @return
	 */
	@RequestMapping("/showUpdate")
	@ResponseBody
	private Object showUpdate(String roleId,HttpSession session){
		logger.info("进入根据ID查询...");
		TbRole manager = roleService.findById(roleId);
		
		session.setAttribute("showRole", manager);
		 
		return manager;


	}
	
	@RequestMapping("/isvaUp")
	@ResponseBody
	private Object isvaUp(TbRole role){
		logger.info("审核..");
		role.setIsva(1);
		
		int num  =roleService.isvaUp(role);
		
		msg.setNum(num);
		if(num>0){
			msg.setMsg("审核成功!");
		}else{
			msg.setMsg("审核失败!");
		}
		
		return msg;
		
	}

	/**
	 * 自行判断 增加 修改<br>
	 * 根据ID是否存在
	 * @param member
	 * @return
	 * @throws JsonProcessingException 
	 * @throws IOException 
	 */
	@RequestMapping("/saveOrUpdate")
	@ResponseBody
	private Object saveOrUpdate(HttpServletRequest request,TbRole role,String str) throws JsonProcessingException {

		logger.info("进入增加修改方法...");

		logger.info(mapper.writeValueAsString(role));

		int num = 0;
		//判断是否存在ID

		//分配模块
		if(!Tool.isEmpty(role.getRoleId())){
			//存在 则修改
			num  = roleService.update(role);

			//根据 角色ID 删除具有的模块
			num = roleModuleService.deleteMod(role.getRoleId());

		}else{
			//反之 增加
			role.setRoleId(Tool.getRandomString(32));
			num = roleService.save(role);
		}


		//增加
		String[] attr = str.split(",");
		for (int i = 0; i < attr.length; i++) {
			TbRoleModule rm = new TbRoleModule();
			rm.setRmId(Tool.getRandomString(32)); 
			rm.setRoleId(role.getRoleId());
			rm.setModId(attr[i]);
			num = roleModuleService.save(rm); 
		}

		//根据 操作返回
		msg.setNum(num);
		if(num>0){
			msg.setMsg("操作成功!");
		}else{
			msg.setMsg("操作失败!");
		}
		return msg;
	}

	@RequestMapping("/findVague")
	@ResponseBody
	private Object findVague(Pages page,TbRole role){
		logger.info("进入模糊查询...");

		List<TbRole> list = roleService.findVague(page, role);


		int num = roleService.findBycount(role);

		//由于前端所需条件
		Map<String, Object> map = new HashMap<>();

		map.put("code", 0);
		map.put("msg","");
		map.put("count",num);
		map.put("data", list);
		return map;
	}

}
